// 模拟Socket类
class Socket {
  write(data: string): void {
    console.log(`Socket sending: ${data}`);
  }
  
  end(): void {
    console.log('Socket closed');
  }
}

// 模拟MQTT类
namespace MQTT {
  export class Client {
    publish(topic: string, data: string): void {
      console.log(`MQTT publishing to ${topic}: ${data}`);
    }
    
    end(): void {
      console.log('MQTT client closed');
    }
  }
}

export interface NetworkAdapter {
    protocol: string;
    connect(config: any): Promise<void>;
    send(data: any): Promise<any>;
    close(): Promise<void>;
    isConnected(): boolean;
}

export class HTTPAdapter implements NetworkAdapter {
    protocol = 'http';
    
    async connect(config: any) {
        // HTTP连接实现
    }

    async send(data: any) {
        // HTTP请求发送
    }

    async close() {
        // 关闭连接
    }

    isConnected() {
        return true; // HTTP是无状态协议
    }
}

export class TCPAdapter implements NetworkAdapter {
  private socket: any;
  private connected = false;

  async connect(): Promise<void> {
    this.socket = new Socket();
    this.connected = true;
  }

  async send(data: string): Promise<void> {
    if (!this.connected) throw new Error('Not connected');
    this.socket.write(data);
  }

  async close(): Promise<void> {
    this.socket.end();
    this.connected = false;
  }

  isConnected(): boolean {
    return this.connected;
  }
    protocol = 'tcp';
    // TCP实现...
}

export class MQTTAdapter implements NetworkAdapter {
  private client: any;
  private connected = false;

  async connect(): Promise<void> {
    this.client = new MQTT.Client();
    this.connected = true;
  }

  async send(data: string): Promise<void> {
    if (!this.connected) throw new Error('Not connected');
    this.client.publish('topic', data);
  }

  async close(): Promise<void> {
    this.client.end();
    this.connected = false;
  }

  isConnected(): boolean {
    return this.connected;
  }
    protocol = 'mqtt';
    // MQTT实现...
}